//
//  JDPayResultViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/8/17.
//

import UIKit

class JDPayResultViewController: JDBaseViewController {

    var payResultStr = ""
    
    var paySuccess: Bool = false
    
    var resultImageView: UIImageView?
    var resultLabel: UILabel?
    var faileReasonLabel: UILabel?
    var faileReasonStr: String?
    
    var checkOrderButton: UIButton?
    var backShopcartButton: UIButton?
     
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.view.backgroundColor = .commonColor()
//        self.setupLeftBackItem()
    }
    
    func setupUIElements() {
        
//        var resultImageView: UIImageView?
        self.resultImageView = UIImageView()
        self.view.addSubview(self.resultImageView!)
        self.resultImageView?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(106)//或者18
            make.size.equalTo(CGSizeMake(132, 132))
        })
        self.resultImageView?.contentMode = .scaleAspectFit
        
//        var resultLabel: UILabel?
        
        self.resultLabel = UILabel()
        self.view.addSubview(self.resultLabel!)
        self.resultLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.resultImageView!.snp.bottom)
            make.height.equalTo(25)
        })
        self.resultLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 18, weight: .semibold), textColor: color("#333333"))
        
        
//        var faileReasonLabel: UILabel?
        self.faileReasonLabel = UILabel()
        self.view.addSubview(self.faileReasonLabel!)
        self.faileReasonLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.resultLabel!.snp.bottom).offset(8)
            make.height.equalTo(20)
        })
        self.faileReasonLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        
//        var faileReasonStr: String?
//        var checkOrderButton: UIButton?
        
        self.checkOrderButton = UIButton(type: .custom)
        self.view.addSubview(self.checkOrderButton!)
        self.checkOrderButton?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.resultLabel!.snp.bottom).offset(63)
            make.size.equalTo(CGSizeMake(300, 54))
        })
        self.checkOrderButton?.layer.cornerRadius = 27
        self.checkOrderButton?.backgroundColor = .white
        self.checkOrderButton?.setTitle("查看订单", for: .normal)
        self.checkOrderButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.checkOrderButton?.setTitleColor(color("#333333"), for: .normal)
        self.checkOrderButton?.addTarget(self, action: #selector(gotoOrderList), for: .touchUpInside)
        
//        var backShopcartButton: UIButton?
        self.backShopcartButton = UIButton(type: .custom)
        self.view.addSubview(self.backShopcartButton!)
        self.backShopcartButton?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.checkOrderButton!.snp.bottom).offset(10)
            make.size.equalTo(CGSizeMake(300, 54))
        })
        self.backShopcartButton?.layer.cornerRadius = 27
        self.backShopcartButton?.backgroundColor = .white
        self.backShopcartButton?.setTitle("返回购物", for: .normal)
        self.backShopcartButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.backShopcartButton?.setTitleColor(color("#333333"), for: .normal)
        self.backShopcartButton?.addTarget(self, action: #selector(backToShopAction), for: .touchUpInside)
        
        if paySuccess {
            // 成功
            self.resultLabel?.text = "支付成功"
            self.faileReasonLabel?.isHidden = true
            self.resultImageView?.image = UIImage(named: "paySuccess")?.withRenderingMode(.alwaysOriginal)
        } else {
            // 失败
            self.resultLabel?.text =  "支付失败"
            self.faileReasonLabel?.text = self.payResultStr.count > 0 ? self.payResultStr : "失败原因，您的支付限额已达上限"
            self.resultImageView?.image = UIImage(named: "payFailure")?.withRenderingMode(.alwaysOriginal)
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    /// 返回购物
    @objc func backToShopAction() {
        let tabbarVC = JDTools.getKeyWindows()?.rootViewController as? JDTabBarController
        tabbarVC?.selectedIndex = 0
        self.navigationController?.popToRootViewController(animated: false)
    }
    
    /// 查看订单
    @objc func gotoOrderList() {
//        self.navigationController?.popToRootViewController(animated: true)
//        DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
//            let tabbarVC = JDTools.getKeyWindows()?.rootViewController as? JDTabBarController
//            tabbarVC?.selectedIndex = 1
//            NotificationCenter.default.post(name: NSNotification.Name("gotoOrderList"), object: nil)
//        }
        
        let orderList = JDOrderListViewController()
        orderList.isFromPaySuccess = true
        if paySuccess {
            // 支付成功 查看待发货订单
            orderList.currentIndex = 2
        } else {
            // 支付失败 查看待付款订单
            orderList.currentIndex = 1
        }
        self.hidesBottomBarPushViewController(orderList)
        
    }
    

}
